/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import DAO.exceptions.NonexistentEntityException;
import DAO.exceptions.PreexistingEntityException;
import DAO.exceptions.RollbackFailureException;
import Entidad.RecursoSimple;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entidad.Tarea;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author David
 */
public class RecursossimpleDAO implements Serializable, DAOinterface {

    public void create(RecursoSimple recursossimple, EntityManager em) throws PreexistingEntityException, Exception {
        try {
            Tarea idtarea = recursossimple.getIdtarea();
            if (idtarea != null) {
                idtarea = em.getReference(idtarea.getClass(), idtarea.getIdtarea());
                recursossimple.setIdtarea(idtarea);
            }
            em.persist(recursossimple);
            if (idtarea != null) {
                idtarea.getRecursossimpleCollection().add(recursossimple);
                idtarea = em.merge(idtarea);
            }
        } catch (Exception ex) {
            if (findRecursossimple(recursossimple.getIdrecursossimples(), em) != null) {
                throw new PreexistingEntityException("Recursossimple " + recursossimple + " already exists.", ex);
            }
            throw ex;
        } 
    }

    public void edit(RecursoSimple recursossimple, EntityManager em) throws NonexistentEntityException, Exception {
        try {
            RecursoSimple persistentRecursossimple = em.find(RecursoSimple.class, recursossimple.getIdrecursossimples());
            Tarea idtareaOld = persistentRecursossimple.getIdtarea();
            Tarea idtareaNew = recursossimple.getIdtarea();
            if (idtareaNew != null) {
                idtareaNew = em.getReference(idtareaNew.getClass(), idtareaNew.getIdtarea());
                recursossimple.setIdtarea(idtareaNew);
            }
            recursossimple = em.merge(recursossimple);
            if (idtareaOld != null && !idtareaOld.equals(idtareaNew)) {
                idtareaOld.getRecursossimpleCollection().remove(recursossimple);
                idtareaOld = em.merge(idtareaOld);
            }
            if (idtareaNew != null && !idtareaNew.equals(idtareaOld)) {
                idtareaNew.getRecursossimpleCollection().add(recursossimple);
                idtareaNew = em.merge(idtareaNew);
            }
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = recursossimple.getIdrecursossimples();
                if (findRecursossimple(id, em) == null) {
                    throw new NonexistentEntityException("The recursossimple with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } 
    }

    public void destroy(Integer id, EntityManager em) throws NonexistentEntityException, RollbackFailureException, Exception {
        try {
            RecursoSimple recursossimple;
            try {
                recursossimple = em.getReference(RecursoSimple.class, id);
                recursossimple.getIdrecursossimples();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The recursossimple with id " + id + " no longer exists.", enfe);
            }
            Tarea idtarea = recursossimple.getIdtarea();
            if (idtarea != null) {
                idtarea.getRecursossimpleCollection().remove(recursossimple);
                idtarea = em.merge(idtarea);
            }
            em.remove(recursossimple);
        } catch (Exception ex) {
            throw ex;
        } 
    }

    public List<RecursoSimple> findRecursossimpleEntities(EntityManager em) {
        return findRecursossimpleEntities(true, -1, -1, em);
    }

    public List<RecursoSimple> findRecursossimpleEntities(int maxResults, int firstResult, EntityManager em) {
        return findRecursossimpleEntities(false, maxResults, firstResult, em);
    }

    private List<RecursoSimple> findRecursossimpleEntities(boolean all, int maxResults, int firstResult, EntityManager em) {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(RecursoSimple.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
    }

    public RecursoSimple findRecursossimple(Integer id, EntityManager em) {
        return em.find(RecursoSimple.class, id);
    }

    public int getRecursossimpleCount(EntityManager em) {
    
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<RecursoSimple> rt = cq.from(RecursoSimple.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        
    }
    
}
